from machine_lib import *
import os
import pandas as pd
# Regular one
s = login()
Running = True
while Running:
    csv_files = [f for f in os.listdir("./fo_alpha_list") if f.endswith('.csv')]
    if csv_files:
        csv_files.sort(reverse=False)
        next_csv_file = csv_files[0]
        print(f"next_csv_file: {next_csv_file}")

        try:
            df_next_csv = pd.read_csv(f"./fo_alpha_list/{next_csv_file}")
        except Exception as e:
            print(f"Error reading {next_csv_file}: {e}")
            continue

        fo_alpha_list = []
        for _, row in df_next_csv.iterrows():
            fo_alpha_list.append((row['alpha'], int(row['decay'])))

        pools = load_task_pool(fo_alpha_list, 10, 10)
        print(f"len of pools = {len(pools)}")

        file_name = next_csv_file.split(".")[0]
        region = file_name.split("_")[0]

        if region == "AMR":
            uni = "TOP600"
        elif region == "JPN":
            uni = "TOP1600"
        elif region == "USA":
            uni = "TOP3000"
        elif region == "ASI":
            uni = "MINVOL1M"
        elif region == "KOR":
            uni = "TOP600"
        elif region == "TWN":
            uni = "TOP500"
        elif region == "HKG":
            uni = "TOP800"
        elif region == "GLB":
            uni = "TOP3000"
        elif region == "CHN":
            uni = "TOP2000U"
        elif region == "EUR":
            uni = "TOP1200"
        
        
        multi_simulate(pools, "FAST", region, uni, 190, file_name)
        # multi_simulate(pools, "SUBINDUSTRY", region, uni, 0, file_name)
        
        try:
            os.remove(f"./fo_alpha_list/{next_csv_file}")
        except Exception as e:
            print(f"Error deleting {next_csv_file}: {e}")
            continue
        
        with open("log.txt", "a") as log_file:
            log_file.write(f"{next_csv_file} done\n")
    else:
        Running = False
        print("No more CSV files to process")
        break

# GLB_fo_model109 182
# ASI_fo_fundmental6 114
# HKG_fo_fundamental6 139
# GLB_fo_fundamental6 70
# HKG_fo_fundamental4 133-220
# JPN_fo_other466 68
# EUR_fo_fundmental6 71
# HKG_fo_analyst14 86
# CHN_fo_analyst14 44 26
# JPN_fo_fundamental65 55
# EUR_fo_fundamental65 15
# EUR_fo_model216 217
# EUR_fo_model109 171
# JPN_fo_analyst37 96
# JPN_fo_fundamental65 309
# USA_fo_fundamental14 168
# EUR_fo_model138 41

